package com.edu.scnu.element;

import java.awt.Graphics;
import java.util.Random;

import javax.swing.ImageIcon;

import com.edu.scnu.controller.GameThread;
import com.edu.scnu.manager.ElementManager;
import com.edu.scnu.manager.GameElement;
import com.edu.scnu.manager.GameLoad;

public class Boss extends ElementObj{
	private long filetime = 50;// 控制子弹间隔时间
	private String Movefx;// 移动方向，只有left和right
	private String type;
	private int mapLevel;
	private int speed = 1;
	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getMapLevel() {
		return mapLevel;
	}

	public void setMapLevel(int mapLevel) {
		this.mapLevel = mapLevel;
	}

	Random rand = new Random();

	@Override
	public void showElement(Graphics g) {
		if (!this.isIshow()) {
			return;
		}
		// TODO 自动生成的方法存根
		g.drawImage(this.getIcon().getImage(),
				this.getX(), this.getY(),
				this.getW(), this.getH(), null);
	}

	@Override
	public ElementObj createElement(String str) {
		// str格式：x,y,类型，时间
		String[] split = str.split(",");
		this.setX(Integer.parseInt(split[0]));
		this.setY(Integer.parseInt(split[1]));
		this.setMapLevel(Integer.parseInt(split[2]));
		this.setBornTime(Long.parseLong(split[3]));
		ImageIcon icon2 = GameLoad.imgMap.get("boss1");// 这里传入的是配置文件中=号前的字符串
		this.setW(icon2.getIconWidth());
		this.setH(icon2.getIconHeight());
		this.setType("1");
		this.setIcon(icon2);
		if (this.getX() >= 300) {
			this.Movefx = "left";
		} else {
			this.Movefx = "right";
		}
		this.setAttack(1);
		this.setHp(50);
		return this;
	}

	/**
	 * @说明 敌人的移动逻辑
	 */
	@Override
	protected void move(long gameTime) {
		if (this.getX() >= 960 - this.getX()) {
			this.Movefx = "left";
		}
		if (this.getX() <= 0) {
			this.Movefx = "right";
		}
		if (this.Movefx.equals("right")) {
			this.setX(this.getX() + this.speed);
			this.setY(Math.abs((int)Math.sqrt(260 * 260 - (this.getX() - 240) * (this.getX()- 240))));
		}
		if (this.Movefx.equals("left")) {
			this.setX(this.getX() - this.speed);
			this.setY(Math.abs((int)Math.sqrt(260 * 260 - (this.getX() - 240) * (this.getX()- 240))));
		}
		this.setY(this.getY());
	}

	/**
	 * @说明 敌人状态改变时图片的刷新
	 */
	@Override
	public void updateImage(long gameTime) {
		super.updateImage(gameTime);
		if (this.getHp() >= 30) {
			this.setIcon(GameLoad.imgMap.get("boss1"));
			this.setH(this.getIcon().getIconHeight());
			this.setW(this.getIcon().getIconWidth());
			this.setType("1");
		}
		else
		{
			this.setIcon(GameLoad.imgMap.get("boss2"));
			this.setH(this.getIcon().getIconHeight());
			this.setW(this.getIcon().getIconWidth());
			this.setType("2");
			this.setSpeed(3);;
			this.filetime = 30;
		}
	}

	/**
	 * @说明 敌人的添加子弹
	 */
	@Override
	protected void add(long gameTime) {
		if (gameTime % filetime == 0) {
			ElementObj obj = GameLoad.getObj("enemyfile");
			ElementObj element = obj.createElement(this.getFile()); // 以后的框架学习中会碰到
			// 会帮助返回对象的实体,并初始化数据
			ElementManager.getManager().addElement(element, GameElement.ENEMYFILE);
			// 如果要控制子弹速度等，还需要代码编写
		}

	}

	/**
	 * @author 14737
	 * @说明 用于获取敌人的位置信息，用于生成子弹
	 * @需要解决的问题 传输格式"x:${横坐标},y:${纵坐标},n:${子弹图片的键，具体参考GameData.pro文件,例“fire”}"
	 * @return
	 */
	public String getFile() { // 定义的方法
		int x = this.getX();
		int y = this.getY();
		String type = this.getType();
		x += this.getW() / 2 - 5;
		y += this.getH() - 5;
		return "x:" + x + ",y:" + y + ",n:" + "efire" + type;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}


	@Override
	public void die(long gameTime) {
		ElementObj item = new GameItem().createElement(this.getX() + "," + this.getY() + "," + (rand.nextInt(7) + 1));
		ElementManager.getManager().addElement(item, GameElement.GAMEITEM);
	}

	@Override
	public void beAttacked(int attack) {
		this.setHp(this.getHp() - attack);
		if (this.getHp()<=0) {
			super.beAttacked(attack);
		}
	}
}
